<?php
function controller_email($config) {
    //var_dump($_POST);
	$mail = new PHPMailer();  
	$mail->IsSMTP();  // telling the class to use SMTP
	$mail->Mailer = "smtp";
	$mail->SMTPAuth = true; // turn on SMTP authentication
	$mail->SMTPDebug = 0;  // debugging: 1 = errors and messages, 2 = messages only
	$subject = "Kirim Pesan";
	$from = "balerupa.contact@gmail.com";
	$from_name = "Balerupa Contact";
	$mail->From = $from;
	$mail->FromName = $from_name;
	//$mail->SetFrom($from, $from_name);
	$mail->Subject  = $subject;
	$to = $_POST["email"];
	$toname = $_POST["name"];
	$to2 = "balerupa.mail@gmail.com";
	$toname2 = "Balerupa Mail";
	$body = "Hi, $toname ! \n";
	$body .= "Anda telah mengirim pesan pada kami. \n\n";
	$body .= "\"".$_POST["msg"]."\"";
	$body .= "\n\nSilakan tunggu balasan dari kami.";
	$body .= "\nTerimakasih ya";
	$body2 = "Ada pesan dari $to :\n\n\"".$_POST["msg"]."\"";
	//HOSTING
	/*
	$mail->Host = "mail.balerupa.com";
	$mail->Port = 25;
	$mail->Username = "contact@balerupa.com"; // SMTP username
	$mail->Password = "apurelab"; // SMTP password 
	*/
	//GMAIL
	$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
	$mail->Host = 'smtp.gmail.com';
	$mail->Port = 587; 
	$mail->Username = "balerupa.contact@gmail.com";  
	$mail->Password = "apurelabx";           
	//$mail->Username = "balerupa.mail@gmail.com";  
	//$mail->Password = "apurelab";
	
	//ini ke client
	$mail->AddAddress($to);
	//$mail->AddAddress($from);
	$mail->AddReplyTo($to2,$toname2);	
	$mail->Body = $body;
	$mail->Send();
	//ini ke admin
	$mail->ClearAllRecipients();
	$mail->AddAddress($to2);
	$mail->AddReplyTo($to,$toname);	
	$mail->Body = $body2;
	$mail->Send();
	
	$_SESSION['emailed'] = "Pesan Anda telah dikirim. Terimakasih";
	
	header('Location: '.$config['home_url']."contact");
}
